package com.example.demo.controller;

import com.example.demo.Enum.ResultStatus;
import com.example.demo.VO.Result;
import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * (User)表控制层
 *
 * @author hjl
 * @since 2020-11-30 19:33:05
 */
@RestController
@RequestMapping("user")
public class UserController {
    /**
     * 服务对象
     */
    @Resource
    private UserService userService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public User selectOne(Integer id) {
        return this.userService.queryById(id);
    }



    @PostMapping("register")
    public Result register(User user){
        if(user.getNikename()==null||user.getNikename().equals(""))
        {
            return Result.failure("昵称不能为空");
        }
        if(user.getUsername()==null||user.getUsername().equals(""))
        {
            return Result.failure("用户名不能为空");
        }
        if(user.getPassword()==null||user.getPassword().equals(""))
        {
            return Result.failure("密码不能为空");
        }
        if(user.getIdentity()==null||user.getIdentity().equals(""))
        {
            return Result.failure("用户类型不能为空");
        }
        User user1=new User();
        User user2=new User();
        user1.setNikename(user.getNikename());
        user2.setUsername(user.getUsername());
        if(userService.queryByExample(user1).size()!=0)
        {
            return Result.failure("昵称重复");
        }
        if(userService.queryByExample(user2).size()!=0)
        {
            return Result.failure("账号重复");
        }
        User aa=userService.insert(user);
        return Result.success(aa);
    }

    @PostMapping("login")
    public Result login(String username,String password){
        if(username==null||username.equals(""))
        {
            return Result.failure("用户名不能为空");
        }
        if(password==null||password.equals(""))
        {
            return Result.failure("密码不能为空");
        }
        User user=new User();
        user.setUsername(username);
        user.setPassword(password);
        if(userService.queryByExample(user).size()==1)
        {
            return Result.success(userService.queryByExample(user).get(0));
        }else
        {
            return Result.failure("账号或密码错误");
        }
    }
    @PostMapping("update")
    public Result update(User user)
    {
        return Result.success(userService.update(user));
    }

    @DeleteMapping("delete")
    public Result delete(User user)
    {
        return Result.success(userService.deleteById(user.getId()));
    }


}